//////////////////////////////////////////////////////////////////////////////////
//	 Copyright 2011 by Yosef Grabivker, ryuho@homenetworksoft.com
//
//   Licensed under the Apache License, Version 2.0 (the "License");
//   you may not use this file except in compliance with the License.
//   You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
//   Unless required by applicable law or agreed to in writing, software
//   distributed under the License is distributed on an "AS IS" BASIS,
//   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//   See the License for the specific language governing permissions and
//   limitations under the License.
//////////////////////////////////////////////////////////////////////////////////

#ifndef TRAFICGRAPH_H_
#define TRAFICGRAPH_H_

#include "def.h"
#include <wx/timer.h>

class CTraficGraph: public wxPanel, public CGraphSize
{
public:
								CTraficGraph(wxWindow* parent, wxWindowID id=wxID_ANY);
	virtual 					~CTraficGraph();
	
	void						SetUpdateInterVal(unsigned int nUpdateTime);
	void						Reset();
	
	void						SetRecv( const wxString&  strIntName, unsigned long long lRecv, time_t tEvent  );
	void						SetTransm(const wxString& strIntName,  unsigned long long  lTransm, time_t tEvent );

	void						SetTimePeriod(int nPer);
	int							GetTimePeriod()			{ return m_nTimePeriodSec; }

	wxString					GetRecvTotal();
	wxString					GetRecvRate();

	wxString					GetSendTotal();
	wxString					GetSendRate();

	void						SetInterface(const wxString& strName);

	void						SetRecvColor(const wxColor& cl);
	void						SetTransmColor(const wxColor& cl);

	virtual int					GetGraphLeftWidth();
	virtual int					GetGraphWidth();
	virtual int					GetGraphColWidth();


protected:
	void						OnSize(wxSizeEvent& event);
	void						OnPaint(wxPaintEvent& event);
	void 						OnTimer(wxTimerEvent& event);
	
private:
	typedef struct tagTRAFFICDATAENTRY
	{
		tagTRAFFICDATAENTRY():bCompressed(false)		{}

		bool				bCompressed;
		time_t		  		t;
		unsigned long long 	data;
	}TRAFFICDATAENTRY;
	
	typedef std::list<TRAFFICDATAENTRY>		TRAFFICDATA;
	typedef std::map<wxString,TRAFFICDATA>	TRAFFICDATASTORAGE;
	

	void						AddData( const wxString& strIntName, TRAFFICDATASTORAGE& data, unsigned long long lVal, time_t tEvent);
	unsigned long				GetMax( const TRAFFICDATA& data );
	//void						ConvertToBandwidth(TRAFFICDATA& data,	BANDWIDTHDATA& bndwdata);
	void						RemoveOldValues(TRAFFICDATA& data, time_t tExpired);

	
private:
	void						DrawGrid( class wxBufferedPaintDC& dc, unsigned int lDataMax, wxRect&  rectGraphArea);
	void						DrawGridLines(class wxBufferedPaintDC& dc, int x, int y, int w, int h);
	void						DrawTimeLabels(class wxBufferedPaintDC& dc, int x, int y, int w, int h);
	void						DrawTraphicLabels(class wxBufferedPaintDC& dc, int x, int y, int w, int h, unsigned int lDataMax);
	void						DrawGraph(class wxBufferedPaintDC& dc, const TRAFFICDATA& data, const wxRect& rect, const wxColor& color, wxRegion& rg);

	unsigned int				CalculateUpLevel(unsigned int lDataMax);
	void						CalculateBlockParams(int nLineLength, int* nBlocksMap,int nMapSize, int nBlockMinSize, int &nBlockSize, int& nBlockCount);
	bool						GetDistance(const TRAFFICDATAENTRY& entry1, const TRAFFICDATAENTRY& entry2, int& x, int& y);

	wxString					FormatLabel( unsigned long lVal, unsigned long lMax);
	wxString					FormatTime(int nTimeBlockIndex);
	unsigned long 				GetScale(unsigned long lMax);
	unsigned long				GetCurBandwidth(const TRAFFICDATA& data);

	const TRAFFICDATA&			GetInterfaceData( const TRAFFICDATASTORAGE& storage );
	void						CompressList( TRAFFICDATA& data );
	
private:
	wxTimer						m_timer;
	unsigned int 				m_nUpdateTime;
	
	//absolute values(i.e. bytes send)
	TRAFFICDATASTORAGE			m_lstReceive;
	TRAFFICDATASTORAGE			m_lstTransmith;

	int							m_nTrafBlockSize;
	int							m_nTrafBlockCount;

	int							m_nTimeBlockSize;
	int							m_nTimeBlockCount;
	unsigned int				m_lUpLevel;
	int							m_nTimePeriodSec;

	wxString					m_strCurIntName;
	wxColor						m_clrRecv;
	wxColor						m_clrTransm;
};

#endif /*TRAFICGRAPH_H_*/
